<?php
    if (! class_exists('NaturalExtension_Utils')) {
        class NaturalExtension_Utils {
            public $config = array(
                'version'       => '1.4',
                'devil_mode'    => 'true',
                'opt_prefix'    => '_natext_',
                'paths' => array(
                    'js' => array(
                        'less'                          => '/js/vendor/less-min.js',
                        'underscore'                    => '/js/vendor/underscore-min.js',
                        'amplify'                       => '/js/vendor/amplify-min.js',
                        'natext'                        => '/js/natext.js',
                        'natext_page_builder'           => '/js/natext-page-builder.js',
                        'natext_smm_builder'            => '/js/natext-smm-builder.js',
                        'natext_bookshelf'              => '/js/natext-bookshelf.js',
                        'natext_bookshelf_Bookshelf'    => '/js/natext-bookshelf-Bookshelf.js',
                        'natext_bookshelf_Editor'       => '/js/natext-bookshelf-Editor.js',
                        'natext_excerpt'                => '/js/natext-excerpt.js'
                    ),
                    'less' => array(
                        'patch'     => '/less/patch.less'
                    ),
                    'css' => array(
                        'patch'     => '/css/patch.css',
                        'excerpt'   => '/css/excerpt.css'
                    ),
                    'inc' => array(
                        'PageBuilder'       => '/inc/PageBuilder.php',
                        'SMMBuilder'        => '/inc/SMMBuilder.php',
                        'Bookshelf'         => '/inc/Bookshelf.php',
                        'Shortcodes'        => '/inc/Shortcodes.php',
                        'Settings'          => '/inc/Settings.php',
                        'DefaultActions'    => '/inc/DefaultActions.php'
                    ),
                    '3party' => array(
                        'markdown'          => '/inc/3party/markdown.php'
                    ),
                    'wiki' => array(
                        'admin'             => '/inc/other/wiki-admin.md',
                        'author'            => '/inc/other/wiki-author.md',
                        'changelog'         => '/inc/other/changelog.md'
                    ),
                    'ajax' => array(
                        'PageBuilder'       => '/inc/ajax/PageBuilder.php',
                        'SMMBuilder'        => '/inc/ajax/SMMBuilder.php',
                        'Bookshelf'         => '/inc/ajax/Bookshelf.php'
                    ),
                    'img' => array(
                        'ico_menu'  => '/images/icons/icon-16x16.png',
                        'ico_root'  => '/images/icons/icon-32x32.png'
                    )
                ),
                'required_plugins' => array(
                    'anti_spam' => array(
                        array(
                            'name'          => 'Growmap Anti Spambot Plugin',
                            'plugin_dir'    => 'growmap-anti-spambot-plugin/growmap-anti-spambot-plugin.php'
                        ),
                        array(
                            'name'          => 'Bad Behavior',
                            'plugin_dir'    => 'bad-behavior/bad-behavior-wordpress.php'
                        )
                    )
                )
            );


            // Public Functions
            // ------------------------------

            public function load_file($base, $file) {
                require_once($this->get_path($base, $file, OF_FILEPATH));
            }

            public function get_path($base, $file, $root = OF_DIRECTORY) {
                return $root . '/patch' . $this->config['paths'][$base][$file];
            }

            public function nq_scripts() {
                foreach (func_get_args() as $script) {
                    // handle, file, deps, version, in_footer
                    wp_enqueue_script(
                        $script[0],
                        $this->get_path('js', $script[1]),
                        ! empty($script[2]) ? $script[2] : '',
                        ! empty($script[3]) ? $script[3] : false,
                        ! empty($script[4]) ? $script[4] : true
                    );
                }
            }

            public function nq_styles() {
                foreach (func_get_args() as $style) {
                    // handle, path
                    wp_enqueue_style(
                        $style[0],
                        $this->get_path('css', $style[1])
                    );
                }
            }

            public function get_option($name) {
                return get_option($this->config['opt_prefix'] . $name);
            }

            public function set_option($option, $value) {
                $the_option = $this->get_option($option, 'no-option');

                if ($the_option == 'no-option') {
                    add_option($this->config['opt_prefix'] . $option, $value, '', 'yes');
                } elseif ($the_option != $value) {
                    update_option($this->config['opt_prefix'] . $option, $value);
                }
            }

            function _wp_update_option($option, $value) {
                $the_option = get_option($option, 'no-option');

                if ($the_option == 'no-option') {
                    add_option($option, $value, '', 'yes');
                } elseif ($the_option != $value) {
                    update_option($option, $value);
                }
            }

            public function get_page_id_via_name($name) {
                $pages = get_pages();

                foreach ($pages as $page) {
                    if ($page->post_name == $name) {
                        return $page->ID;
                    }
                }

                return NULL;
            }

            public function get_page_menu_id($page_id, $menu_slug = 'header-menu') {
                $menu_locations = get_nav_menu_locations();
                $header_menu =  wp_get_nav_menu_object($menu_locations[$menu_slug]);

                $nav_menus = wp_get_nav_menu_items($header_menu->term_id);

                foreach ((array) $nav_menus as $menu_item) {
                    if ($menu_item->object_id == $page_id) {
                        return $menu_item->ID;
                    }
                }

                return 0;
            }

            public function get_bookshelf_items() {
                $book_page = $this->get_page_id_via_name('book');
                $book_children = array();

                if (! empty($book_page)) {
                    $book_children = get_children(array(
                        'post_parent' => $book_page,
                        'order' => 'ASC',
                        'orderby' => 'ID',
                        'post_status' => 'publish'
                    ));
                }

                return $book_children;
            }

            public function show_message($title, $message, $error_msg = false) {
                $message_tpl   = '<div id="message" class="%s"><h3>%s</h3><p>%s</p></div>';
                $message_class = $error_msg ? 'error' : 'updated fade';

                echo sprintf($message_tpl, $message_class, $title, $message);
            }

            public function display_wiki($wiki_page) {
                $markdown = file_get_contents($this->get_path('wiki', $wiki_page));

                echo Markdown($markdown);
            }
        }
    }
?>